// pages/collect/collect.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectList:[],
        page:1,
        time:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    getUserCollect(){
        let collectData = wx.cloud.database().collection('collect_data')
        let skipnum = (this.data.page-1)*5
        collectData.orderBy('coltime','dec').limit(5).skip(skipnum).get().then(
            res=>{
                let collectList = this.data.collectList
                collectList = collectList.concat(res.data)
                let arr = []
                for(let i = 0;i<res.data.length;i++){
                    arr.push(this.setTime(res.data[i].duration))
                }
                this.setData({
                    collectList:collectList,
                    time:arr
                })
            }
        )
    },
    onLoad: function (options) {
       this.getUserCollect()
    },
    setTime(val){
        let m =parseInt(val/60)
        let s =parseInt(val%60)
        m=m<10?"0"+m:m
        s=s<10?"0"+s:s
        return m+':'+s
    },
    clear(){
        wx.cloud.callFunction({
            name:"manage_collect",
            data:{
                action:"clear"
            }
        }).then(res=>{
            console.log("清空结果",res);
            if(res.result.result.stats.removed){
                wx.showToast({
                  title: '删除了'+res.result.result.stats.removed+'条数据',
                })
                this.setData({
                    collectList:[]
                })
            }
        })
    },

    onReachBottom: function () {
        // console.log(1);
        let page = this.data.page
        ++page;
        this.setData({
            page:page
        })
        this.getUserCollect();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})